/*
 * Copyright 2010 Brian Watkins
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package bw.util;

import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import bw.news.NewsServant;
import bw.weather.WeatherServant;
import bw.search.SearchServant;
import bw.theme.ThemeServant;
import bw.refresh.RefresherServant;
import bw.os.*;

public class AppManager implements ServletContextListener
{
    private Vector _servants = new Vector();

    public AppManager ()
    {
        //nothing
    }

    public void contextInitialized ( ServletContextEvent sce )
    {
        Log.getInstance().write( "AppManager: Starting up!" );

        ServletContext context = sce.getServletContext();

        //init the proper timezone
        String tzcode = context.getInitParameter( "wd.timezone" );
        DateFactory.getInstance().setTimeZone( tzcode );

        //here's where you instantiate the servants
        //NOTE: ObjectStore must be first since others depend on it!
        _servants.addElement( new ObjectStoreServant() );
        _servants.addElement( new ThemeServant() );
        _servants.addElement( new SearchServant() );
        _servants.addElement( new NewsServant() );
        _servants.addElement( new WeatherServant() );

        //NOTE: RefresherServant should probably be last because it will
        //kick off the refreshing data source pool that all the specific
        //updating data sources depend on (ie News, Weather, etc)
        _servants.addElement( new RefresherServant() );

        //here's where you call init
        Log.getInstance().write( "AppManager: Calling init on servants!" );
        for ( Enumeration e = _servants.elements(); e.hasMoreElements(); )
        {
            Servant servant = (Servant) e.nextElement();
            Log.getInstance().write( "Starting servant: " + servant.getClass().getName() );
            servant.init( context );
        }
    }

    public void contextDestroyed ( ServletContextEvent sce )
    {
        Log.getInstance().write( "AppManager: Calling shutdown on servants!" );

        //call shutdown on the servants -- do this in reverse order
        Collections.reverse( _servants );

        for ( Enumeration e = _servants.elements(); e.hasMoreElements(); )
        {
            Servant servant = (Servant) e.nextElement();
            Log.getInstance().write( "Shutting down servant: " + servant.getClass().getName() );
            servant.shutdown( sce.getServletContext() );
        }
        
        Log.getInstance().write( "AppManager: Goodbye cruel world!" );
    }
}